<?php

namespace App\Http\Controllers\Api\Framework\Common;

use App\Http\Controllers\Controller;
use App\Master\Framework\Library\Uploads;
use App\Utils\AppResult;
use Illuminate\Http\Request;

/**
 * 公共
 * PassportController
 * Class WxController
 * @package App\Http\Controllers\Api\v1
 */
class UploadController extends Controller
{
    /**
     * 公共接口，文件上传
     *
     * @param Request $request
     * @param string suffix 文件后缀
     * @param string size 文件限制大小
     * @return string|void
     */
    public function file(Request $request)
    {
        $res = (Uploads::getInstance('file', [
            'suffix' => $request->suffix ?? 'pdf,doc,docx,xls,xlsx,xlsm,xlt,xltx,xltm,csv',
            'size'   => intval($request->size ?? 2)
        ]))->init()->getMessage();

        if ($res['code'] != 200) {
            return AppResult::response201($res['msg']);
        }

        return AppResult::response200($res['msg'], $res['data']);
    }

    /**
     * 公共接口，文件上传
     *
     * @param Request $request
     * @param string suffix 文件后缀
     * @param string size 文件限制大小
     * @return string|void
     */
    public function image(Request $request)
    {
        $res = (Uploads::getInstance('image', [
            'suffix' => $request->suffix ?? 'jpg,png,bmp,jpeg,gif',
            'size'   => intval($request->size ?? 2)
        ]))->init()->getMessage();

        if ($res['code'] != 200) {
            return AppResult::response201($res['msg']);
        }

        return AppResult::response200($res['msg'], $res['data']);
    }

    /**
     * 公共接口，文件上传
     *
     * @param Request $request
     * @param string suffix 文件后缀
     * @param string size 文件限制大小
     * @return string|void
     */
    public function video(Request $request)
    {
        $res = (Uploads::getInstance('video', [
            'suffix' => $request->suffix ?? 'wmv,asf,asx,rm,rmvb,mp4,3gp,mov,m4v,avi,dat,mkv,flv,vob',
            'size'   => intval($request->size ?? 2)
        ]))->init()->getMessage();

        if ($res['code'] != 200) {
            return AppResult::response201($res['msg']);
        }

        return AppResult::response200($res['msg'], $res['data']);
    }

    /**
     * 公共接口 云上传token
     * @return string
     */
    public function ossToken()
    {
        $res = (Uploads::getInstance('image'))->token()->getMessage();
        if ($res['code'] != 200) {
            return AppResult::response201($res['msg']);
        }
        return AppResult::response200('success', $res['data']);
    }
}
